package com.waitingresult.str;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: WangShuai
 * @CreateTime: 2024-07-02  10:53
 * @Description:
 * @Version: 1.0
 */
public class ValidStr {
    public static List<Character> leftList = Arrays.asList('(', '[', '{');

    public static void main(String[] args) {
        System.err.println(isValid("([)]"));
        //()[]
    }

    public static boolean isValid(String s) {

        HashMap<Character, Integer> leftMap = new HashMap<>();
        HashMap<Character, Integer> rightMap = new HashMap<>();

        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (leftList.contains(c)) {
                if (leftMap.containsKey(c)) {
                    leftMap.put(c, leftMap.get(c) + 1);
                } else {
                    leftMap.put(c, 1);
                }
            } else {
                if (rightMap.containsKey(c)) {
                    rightMap.put(c, rightMap.get(c) + 1);
                } else {
                    rightMap.put(c, 1);
                }
            }
        }


        if (leftMap.get('(') == null && rightMap.get(')') != null) {
            return false;
        }
        if (leftMap.get('[') == null && rightMap.get(']') != null) {
            return false;
        }
        if (leftMap.get('{') == null && rightMap.get('}') != null) {
            return false;
        }


        if (rightMap.get('(') == null && leftMap.get(')') != null) {
            return false;
        }
        if (rightMap.get('[') == null && leftMap.get(']') != null) {
            return false;
        }
        if (rightMap.get('{') == null && leftMap.get('}') != null) {
            return false;
        }

        return true;
    }
}
